
package com.wobaby.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.wobaby.util.RenditionUncheckedException;
import com.wobaby.util.ValueObjectTransformer;
import com.wobaby.util.log.Log;
import com.wobaby.util.log.LogPrinter;
import com.wobaby.util.xml.XMLDocument;
import com.wobaby.util.xml.XMLElement;

// Referenced classes of package com.wobaby.security:
//            PermissionEntry, Checker

public class PermissionMatrix
{

    public PermissionMatrix()
    {
    }

    public static void readPermissions()
    {
        init();
    }

    public static PermissionEntry getActionByResourceName(String resourceName)
    {
        resourceName = Checker.trimResourceName(resourceName);
        PermissionEntry p = (PermissionEntry)resourceLookup.get(resourceName);
        if(authTrace)
            Log.getLog("auth").trace(com.wobaby.security.PermissionMatrix.class, "Get action by resource name : " + resourceName + ", found: " + (p != null));
        return p;
    }

    public static PermissionEntry getActionByCommandName(String commandName)
    {
        PermissionEntry p = (PermissionEntry)nameLookup.get(commandName);
        if(authTrace)
            Log.getLog("auth").trace(com.wobaby.security.PermissionMatrix.class, "Get action by commandName: " + commandName + ", found: " + (p != null));
        return p;
    }

    public static PermissionEntry getActionByName(String name)
    {
        PermissionEntry p = getActionByCommandName(name);
        if(p == null)
            p = getActionByResourceName(name);
        return p;
    }

    public static List getDefinedCommands()
    {
        return actions;
    }


    public static List getPermissionActions()
    {
        if(actions.size() == 0)
            init();
        return actions;
    }
    
    public static List getOriginalActions()
    {
        if(originalActions.size() == 0)
            init();
        return originalActions;
    }

    private static void init()
    {
        authLog = Log.getLog("auth");
        authTrace = authLog.configuredFor(0);
        XMLDocument doc = null;
        XMLElement root = null;
        actions.clear();
        originalActions.clear();
        nameLookup.clear();
        resourceLookup.clear();
        try
        {
            java.io.InputStream ins = (com.wobaby.security.PermissionMatrix.class).getResourceAsStream("Matrix");
            doc = new XMLDocument(ins);
            root = doc.getCoreElement();//get the root element("<permission-definition>")
            List items = root.getSubelements("PermissionEntry");
            if(items == null || items.size() == 0)
                throw new RenditionUncheckedException("No commands defined");
            for(int i = 0; i < items.size(); i++)
            {
                XMLElement fe = (XMLElement)items.get(i);
                PermissionEntry f = new PermissionEntry();
                ValueObjectTransformer.XMLElement2VO(fe, f);
                originalActions.add(f);
                String mso = "";
                if(fe.getAttribute("mdp") != null)
                    mso = mso + "M";
                if(fe.getAttribute("sp") != null)
                    mso = mso + "S";
                if(fe.getAttribute("op") != null)
                    mso = mso + "O";
                f.permLetter = mso;
                List resources = new ArrayList(10);
                List optionElements = fe.getSubelements("resource");
                if(optionElements != null && optionElements.size() > 0)
                {
                    for(int k = 0; k < optionElements.size(); k++)
                    {
                        XMLElement tmp = (XMLElement)optionElements.get(k);
                        String r = tmp.getText(true);
                        if(r != null && r.length() > 0)
                        {
                            resources.add(r);
                            resourceLookup.put(r, f);
                        }
                    }

                    f.resources = resources;
                }
                List depends = new ArrayList(10);
                optionElements = fe.getSubelements("depends");
                if(optionElements != null && optionElements.size() > 0)
                {
                    for(int k = 0; k < optionElements.size(); k++)
                    {
                        XMLElement tmp = (XMLElement)optionElements.get(k);
                        String r = tmp.getText(true);
                        if(r != null && r.length() > 0)
                            depends.add(r);
                    }

                    f.setDependencies(depends);
                }
                actions.add(f);
                nameLookup.put(f.getName(), f);
                if(authTrace)
                    Log.getLog("auth").trace(com.wobaby.security.PermissionMatrix.class, "Load action def: " + f);
            }

        }
        catch(RuntimeException re)
        {
            throw re;
        }
        catch(Exception e)
        {
            throw new RenditionUncheckedException("Exception in ResourceMaster()", e);
        }
    }

    protected static LogPrinter authLog;
    protected static boolean authTrace;
    private static List actions = new ArrayList();
    private static List originalActions = new ArrayList();
    private static HashMap nameLookup = new HashMap();
    private static HashMap resourceLookup = new HashMap();
    
    static 
    {
        readPermissions();
    }
}